package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void addCart(ShoppingCartDTO dto) {
        //创建shoppingcart对象
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(dto, cart); //浅拷贝
        //1、补充缺失的属性
        //判断该商品是否已存在于购物车 条件：dishId+dishFlavor+userId
        //只查当前用户自己的购物车
        cart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart1 = shoppingCartMapper.selectBy(cart);
        if (cart1 == null) {
            //代表购物车没有改商品数据
            // 判断是新增套餐还是新增菜品
            if (dto.getDishId() != null){
                //新增的是菜品,获取菜品相关信息
                Dish dish = dishMapper.selectById(dto.getDishId());
                cart.setName(dish.getName());
                cart.setAmount(dish.getPrice());
                cart.setImage(dish.getImage());

            }else{
                //新增的是套餐
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                cart.setName(setmeal.getName());
                cart.setAmount(setmeal.getPrice());
                cart.setImage(setmeal.getImage());
            }
            cart.setNumber(1); //到底是1还是加一？判断该商品是否已经存在于购物车
            cart.setCreateTime(LocalDateTime.now());

            // 3.将商品数据存入shoppingcart表中
            shoppingCartMapper.insert(cart);
        }else {
            // 4、购物车有数据，将原来购物车的商品数+1,调用mapper更新方法
            cart1.setNumber(cart1.getNumber() + 1);
            shoppingCartMapper.update(cart1);
        }


        //最终目的：将用户添加的商品，存入到购物车表shopping_cart中
    }

    @Override
    public List<ShoppingCart> list() {
        //只能查自己名下的购物车
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    @Override
    public void clean() {
        // 只能清空自己名下的购物车
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO dto) {


        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(dto, cart);
        ShoppingCart cart1 = shoppingCartMapper.selectBy(cart); //拿到那条数据
        int number = cart1.getNumber();
        if(number - 1 != 0){
            cart1.setNumber(number - 1);
            //更新数量
            shoppingCartMapper.update(cart1);
        }else {
            shoppingCartMapper.delete(cart1.getId());
        }

    }
}
